package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*****
 * 阻塞线程的结果方法
 *
 */
class TcpServer {

	public static void main(String[] args) throws IOException {
		thread();

	}
	
	/****
	 * 阻塞问题
	 */
	public static void socket() throws IOException {
		System.out.println("socket tcp服务器端启动....");
		ServerSocket serverSocket = new ServerSocket(9090);
		// 等待客户端请求
		try {
			while (true) {
				final Socket accept = serverSocket.accept();
				try {
					InputStream inputStream = accept.getInputStream();
					// 转换成string类型
					byte[] buf = new byte[1024];
					int len = inputStream.read(buf);
					String str = new String(buf, 0, len);
					System.out.println("服务器接受客户端内容:" + str);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			serverSocket.close();
		}
	}
	
	public static void thread() throws IOException {
		System.out.println("socket tcp服务器端启动....");
		ServerSocket serverSocket = new ServerSocket(9090);
		// 等待客户端请求
		try {
			while (true) {
				final Socket accept = serverSocket.accept();
				new Thread(new Runnable() {
					
					public void run() {
						try {
							InputStream inputStream = accept.getInputStream();
							// 转换成string类型
							byte[] buf = new byte[1024];
							int len = inputStream.read(buf);
							String str = new String(buf, 0, len);
							System.out.println("服务器接受客户端内容:" + str);
						} catch (Exception e) {
						} 
						
					}
				}).start();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			serverSocket.close();
		}
	}
	
	/****
	 * 使用线程池的方式解决阻塞问题
	 * @throws IOException
	 */
	public static void newCachedThreadPool() throws IOException {
		System.out.println("socket tcp服务器端启动....");
		ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
		ServerSocket serverSocket = new ServerSocket(9090);
		// 等待客户端请求
		try {
			while (true) {
				final Socket accept = serverSocket.accept();
				newCachedThreadPool.execute(new Runnable() {

					public void run() {
						try {
							InputStream inputStream = accept.getInputStream();
							// 转换成string类型
							byte[] buf = new byte[1024];
							int len = inputStream.read(buf);
							String str = new String(buf, 0, len);
							System.out.println("服务器接受客户端内容:" + str);
						} catch (Exception e) {
							// TODO: handle exception
						}

					}
				});

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			serverSocket.close();
		}
	}

}

public class TcpClient {
	public static void main(String[] args) throws UnknownHostException, IOException {
		System.out.println("socket tcp 客户端启动....");
		Socket socket = new Socket("127.0.0.1", 9090);
		OutputStream outputStream = socket.getOutputStream();
		outputStream.write("我是客户端".getBytes());
		socket.close();
	}
}

